package com.tzbank.wisdom.service.impl;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.AmountUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.DateUtils;
import com.tzbank.wisdom.constant.SysConstans;
import com.tzbank.wisdom.service.CommonService;
import com.tzbank.wisdom.service.SaveService;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Service(value = "SaveService")
@Slf4j
public class SaveServiceImpl implements SaveService {
	
	@Autowired
    private CommonService commonService;
	
	/**
	 * @param 获取公共参数
	 * @return
	 */
	public Map<String, Object> getInputMap(Map<String, String> reqBody) {

		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> maps =  JSONObject.fromObject(reqBody);
		inputMap = RequestMap.getInputMap(maps);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
		inputMap.putAll(userMap);
		return inputMap;
	}
	
	/**
	 * 
	 * 储蓄活转定
	 *
	 */
	@Override
	public Map<String, Object> currentToFDTransfer(Map<String, String> reqBody) {
		log.info("活转定请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		Map<String, Object> maps = commonService.verifyTransPassword(reqBody);
		if (!CodeUtil.CODE_200.getCode().equals(maps.get("code"))) {
			maps.put("code", CodeUtil.CODE_10021.getCode());
			maps.put("msg", CodeUtil.CODE_10021.getName());
			log.info("校验交易密码 响应结果:{}", maps.get("msg"));
			return maps;
		}
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_CURRENTTOFD.getServiceCode(), TransCodeEnum.TRANS_CURRENTTOFD.getServiceSence(), 
		"", "", "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		//如果查余额 根据卡号列表去核心查余额
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("acctType", "PSAV");        //活期账户类型  登录的时候返回的有值
		inputMap.put("acctNo",reqBody.get("acctNo") );  //活期账号  登录的时候返回的有值 ----   "6224271190391118-101"
		inputMap.put("ccy", "CNY");   //币种 写死
		inputMap.put("caTtFlag", "C"); // 钞汇标志  写死
		inputMap.put("productType", reqBody.get("productType")); //存款类型 （定期账户、通知存款、定活两便、财运通、小额财运通      ）---"TIM"
		inputMap.put("depTerm", reqBody.get("depTerm"));//存期 ----"M3"
		inputMap.put("intRate",   reqBody.get("intRate"));//利率  核心接口查询  -----"1.320000%"
		inputMap.put("tranAmt",   reqBody.get("tranAmt"));  //交易金额  ---"111"
		inputMap.put("tranOutAcctName", reqBody.get("tranOutAcctName"));  //活期账户客户名字  登录的时候返回的有值 --"缘尽"
		inputMap.put("autoRenewRollover", "1");  //自动转存标志 写死 1
		
		
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 活转定结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("活转定响应"+JSONObject.fromObject(result));
			return result;
		}
		 JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		 dataJson.remove("head");
		 result.put("data", dataJson);
		 log.info("活转定响应"+JSONObject.fromObject(result));
		
		return result;

	}

	/**
	 * 
	 * 储蓄查询活期账户列表和余额
	 *
	 */
	@Override
	public Map<String, Object> getAcctList(Map<String, String> reqBody) {
		log.info("查询活期账户列表和余额请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_ACTNOINFO.getServiceCode(), TransCodeEnum.TRANS_ACTNOINFO.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("queryType", "0");  //查询类型  0全部
		inputMap.put("currentNum", "");   //当前记录号
		inputMap.put("totalNum", ""); // 本页记录总数
		//获得登录时的账号列表 从Redis中去取+++++++++++++++++++++++++++++  这一步的取值需要测试
		JSONArray loginList = new JSONArray();
		if (!StringUtils.isEmpty((String)reqBody.get("clientSeqNo"))) {
			Map<?, ?> userMaps = RedisUtils.getUserInfo((String)reqBody.get("clientSeqNo"));
			if (userMaps != null) {
				loginList= JSONArray.fromObject(userMaps.get("acctArray"));
			}
		}
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 活转定结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			log.info("查询活期账户列表和余额响应"+JSONObject.fromObject(result));
			return result;
		}
		//返回的是全部账户的列表
	    JSONObject dataJson = JSONObject.fromObject(result.get("data"));
	    JSONArray acctJsonUser = (JSONArray) dataJson.get("listArray");
	    //所有账户和 登录的账户比对进行筛选
	    JSONArray actAndBalArray = new JSONArray();
	    for(int i=0;i<acctJsonUser.size();i++) {
    		for(int j=0;j<loginList.size();j++) {
    			//拼接用户所有账号返回的账号   ++++++++++++++++++++++++  这一步的取值需要测试
    			String acctNoUser="";
    			if(!acctJsonUser.getJSONObject(i).getString("acctSerialNo").equals("0")) {
    				acctNoUser = acctJsonUser.getJSONObject(i).getString("acctNo").trim()+"-"+acctJsonUser.getJSONObject(i).getString("acctSerialNo").trim();	
    			}else{
    				acctNoUser= acctJsonUser.getJSONObject(i).getString("acctNo").trim();
    			}
    			if(loginList.getJSONObject(j).getString("acctType").equals("PSAV")&&loginList.getJSONObject(j).getString("acctNo").equals(acctNoUser)) {
    				JSONObject acctOne =new JSONObject();
    				acctOne.put("acctNo", acctNoUser);
    				acctOne.put("balance", acctJsonUser.getJSONObject(i).getString("balance"));
    				acctOne.put("acctName", acctJsonUser.getJSONObject(i).getString("acctName"));
    				acctOne.put("certNo", acctJsonUser.getJSONObject(i).getString("certNo"));
    				/***/	
    				acctOne.put("acctLevel", acctJsonUser.getJSONObject(i).getString("acctLevel"));
    				acctOne.put("cardAcctType", acctJsonUser.getJSONObject(i).getString("cardAcctType"));
    				acctOne.put("openBranch", acctJsonUser.getJSONObject(i).getString("openBranch"));
    				acctOne.put("acctType", acctJsonUser.getJSONObject(i).getString("acctType"));
    				
    				actAndBalArray.add(acctOne);
    			}
    		}
    	}
	    dataJson.remove("listArray");
	    dataJson.remove("head");
	    dataJson.put("listArray", actAndBalArray);
	    result.remove("data");
	    result.put("data", dataJson);
	    log.info("查询活期账户列表和余额响应"+JSONObject.fromObject(result));
		return result;

	}
    
	
	/**
	 * 
	 * 储蓄 -客户其下定期产品的列表
	 * @param reqBody
	 * @return
	 */
	public Map<String, Object> getFixProductList(Map<String, String> reqBody){
		log.info("客户其下定期产品的列表请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_ACTNOINFO.getServiceCode(), TransCodeEnum.TRANS_ACTNOINFO.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("queryType", "0");  //查询类型  0全部
		inputMap.put("currentNum", "");   //当前记录号
		inputMap.put("totalNum", ""); // 本页记录总数
		//获得登录时的账号列表 从Redis中去取
		JSONArray loginList = new JSONArray();
		if (!StringUtils.isEmpty((String)reqBody.get("clientSeqNo"))) {
			Map<?, ?> userMaps = RedisUtils.getUserInfo((String)reqBody.get("clientSeqNo"));
			if (userMaps != null) {
				loginList= JSONArray.fromObject(userMaps.get("acctArray"));
			}
		}
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); 
		 if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			    log.info("客户其下定期产品的列表响应"+JSONObject.fromObject(result));
				return result;
			}
		JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		//返回的是全部账户的列表
	    JSONArray acctJsonUser = (JSONArray) dataJson.get("listArray");		
	    //所有账户和 登录的账户比对进行筛选
	    JSONArray acctJsonUserNew = new JSONArray();
		for(int i=0;i<acctJsonUser.size();i++) {
    		//首先去掉产品类型为活期的
    		if(!"1".equals(acctJsonUser.getJSONObject(i).getString("productType"))&&StringUtils.isNotBlank(acctJsonUser.getJSONObject(i).getString("productType"))) {
    			//拼接账户++++++++++  这一步的取值需要测试
    			String acctNoUser="";
    			if(!acctJsonUser.getJSONObject(i).getString("acctSerialNo").equals("0")) {
    				acctNoUser = acctJsonUser.getJSONObject(i).getString("acctNo").trim()+"-"+acctJsonUser.getJSONObject(i).getString("acctSerialNo").trim();	
    			}else{
    				acctNoUser= acctJsonUser.getJSONObject(i).getString("acctNo").trim();
    			}
    			for(int j=0;j<loginList.size();j++) {
					 if(loginList.getJSONObject(j).getString("acctNo").equals(acctNoUser)&&loginList.getJSONObject(j).getString("acctType").equals("PTIM")) {
						 //去掉页面不需要的字段
			   			 acctJsonUser.getJSONObject(i).remove("acctNo");
			   			 acctJsonUser.getJSONObject(i).remove("acctSerialNo");
			   			 acctJsonUser.getJSONObject(i).put("acctNo", acctNoUser);
			   			 acctJsonUser.getJSONObject(i).remove("openBranch");
			   			 acctJsonUser.getJSONObject(i).remove("accoAcctNo");
			   			 acctJsonUser.getJSONObject(i).remove("glCode");
			   			 acctJsonUser.getJSONObject(i).remove("cardPbFlag");
			   			 acctJsonUser.getJSONObject(i).remove("certType");
			   			 acctJsonUser.getJSONObject(i).remove("actualBal");
			   			 acctJsonUser.getJSONObject(i).remove("blockedAmt");
			   			 acctJsonUser.getJSONObject(i).remove("eleAcctFlag");
			   			 acctJsonUser.getJSONObject(i).remove("acctLevel");
			   			 acctJsonUser.getJSONObject(i).remove("checkStatus");
			   			 acctJsonUser.getJSONObject(i).remove("payMode");
			   			 acctJsonUser.getJSONObject(i).remove("certStatus");
			   			 acctJsonUser.getJSONObject(i).remove("cartAcctType");
			   			 acctJsonUser.getJSONObject(i).remove("controlCode");
			   			 String valueDateStr=DateUtils.getdatefor_yyyy_MM_dd(acctJsonUser.getJSONObject(i).getString("valueDate"));
			   			 String matureDate=DateUtils.getdatefor_yyyy_MM_dd(acctJsonUser.getJSONObject(i).getString("matureDate"));
			   		     acctJsonUser.getJSONObject(i).put("valueDate", valueDateStr);
			   			 acctJsonUser.getJSONObject(i).put("matureDate", matureDate);
						 acctJsonUserNew.add(acctJsonUser.getJSONObject(i));
					 }
				 }
    		} 
    	}
    	// 0--其他   2--普通定期  3--定活两便  4--通知存款   5--财运通  6--小额财运通   7--大额存单  8--特色储蓄(青年储蓄)   9--特色储蓄(养老储蓄)   10--特色储蓄(成长储蓄)
		JSONArray dqArray = new JSONArray();
	    JSONArray dhArray = new JSONArray();
	    JSONArray tzArray = new JSONArray();
	    JSONArray cytArray = new JSONArray();
	    JSONArray xecytArray = new JSONArray();
	    //对新筛选出来的列表做处理
	    for(int i=0;i<acctJsonUserNew.size();i++) {
	    	switch (acctJsonUserNew.getJSONObject(i).getString("productType")) {
	      
	        case  "2":
	        	acctJsonUserNew.getJSONObject(i).put("productTypeName", "普通定期 ");
	        	dqArray.add(acctJsonUserNew.getJSONObject(i));
				break;   
	        case "3":
	        	acctJsonUserNew.getJSONObject(i).put("productTypeName", "定活两便");
	        	dhArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "4":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "通知存款");
				tzArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "5":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "财运通");
				cytArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "6":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "小额财运通");
				xecytArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			
	        }
	    }
    	dataJson.remove("listArray");
    	dataJson.put("dqArray", dqArray);	
    	dataJson.put("dhArray", dhArray);	
    	dataJson.put("tzArray", tzArray);	
    	dataJson.put("cytArray", cytArray);	
    	dataJson.put("xecytArray", xecytArray);	
    	dataJson.remove("head");
	    result.remove("data");
	    result.put("data", dataJson);
	    log.info("客户其下定期产品的列表响应"+JSONObject.fromObject(result));
		return result;
	}
	
	/**
	 * 
	 * 储蓄 -客户其下定期产品的列表----------》为个人中心的定期总额使用
	 * @param reqBody
	 * @return
	 */
	public Map<String, Object> getFixProductList_Ding(Map<String, String> reqBody){
		log.info("请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子报文头入参
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_ACTNOINFO.getServiceCode(), TransCodeEnum.TRANS_ACTNOINFO.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		//客户号和 证件号至少输入一个
		inputMap.put("globalType", reqBody.get("globalType"));        //证件类型
		inputMap.put("globalId", reqBody.get("globalId"));        //证件号码
		inputMap.put("queryType", "0");  //查询类型  0全部
		inputMap.put("currentNum", "");   //当前记录号
		inputMap.put("totalNum", ""); // 本页记录总数
		//获得登录时的账号列表 从Redis中去取+++++++++++++++++++++++++++++  这一步的取值需要测试
		 
		    JSONArray loginList = new JSONArray();
		   
		    if (!StringUtils.isEmpty((String)reqBody.get("clientSeqNo"))) {
				Map<?, ?> userMaps = RedisUtils.getUserInfo((String)reqBody.get("clientSeqNo"));
				if (userMaps != null) {
					loginList= JSONArray.fromObject(userMaps.get("acctArray"));
				}
			}
			
		
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); 
		 if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			    log.info("响应"+JSONObject.fromObject(result));
				return result;
			}
		JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		
		//返回的是全部账户的列表
	    JSONArray acctJsonUser = (JSONArray) dataJson.get("listArray");		
	    //所有账户和 登录的账户比对进行筛选
	    JSONArray acctJsonUserNew = new JSONArray();
		for(int i=0;i<acctJsonUser.size();i++) {
    		
    		//首先去掉产品类型为活期的
    		if(!"1".equals(acctJsonUser.getJSONObject(i).getString("productType"))&&StringUtils.isNotBlank(acctJsonUser.getJSONObject(i).getString("productType"))) {
    			
    				
    			//拼接账户++++++++++  这一步的取值需要测试
    			String acctNoUser="";
    			
    			if(!acctJsonUser.getJSONObject(i).getString("acctSerialNo").equals("0")) {
    				acctNoUser = acctJsonUser.getJSONObject(i).getString("acctNo").trim()+"-"+acctJsonUser.getJSONObject(i).getString("acctSerialNo").trim();	
    			}else{
    				acctNoUser= acctJsonUser.getJSONObject(i).getString("acctNo").trim();
    			}
    			//去掉页面不需要的字段
	   			 acctJsonUser.getJSONObject(i).remove("acctNo");
	   			 acctJsonUser.getJSONObject(i).remove("acctSerialNo");
	   			 acctJsonUser.getJSONObject(i).put("acctNo", acctNoUser);
	   			 acctJsonUser.getJSONObject(i).remove("openBranch");
	   			 acctJsonUser.getJSONObject(i).remove("accoAcctNo");
	   			 acctJsonUser.getJSONObject(i).remove("glCode");
	   			 acctJsonUser.getJSONObject(i).remove("cardPbFlag");
	   			 acctJsonUser.getJSONObject(i).remove("certType");
	   			 acctJsonUser.getJSONObject(i).remove("actualBal");
	   			 acctJsonUser.getJSONObject(i).remove("blockedAmt");
	   			 acctJsonUser.getJSONObject(i).remove("eleAcctFlag");
	   			 acctJsonUser.getJSONObject(i).remove("acctLevel");
	   			 acctJsonUser.getJSONObject(i).remove("checkStatus");
	   			 acctJsonUser.getJSONObject(i).remove("payMode");
	   			 acctJsonUser.getJSONObject(i).remove("certStatus");
	   			 acctJsonUser.getJSONObject(i).remove("cartAcctType");
	   			 acctJsonUser.getJSONObject(i).remove("controlCode");
	   			String valueDateStr=DateUtils.getdatefor_yyyy_MM_dd((String)acctJsonUser.getJSONObject(i).get("valueDate"));
	   			String matureDate=DateUtils.getdatefor_yyyy_MM_dd((String)acctJsonUser.getJSONObject(i).get("matureDate"));
	   		     acctJsonUser.getJSONObject(i).put("valueDate", valueDateStr);
	   			 acctJsonUser.getJSONObject(i).put("matureDate", matureDate);
	   			//是否展示定转活的标识  默认 ："acctPermit", "1" 不展示?????????????????????????????+++++++++++++++++++++   这个需要确认
    	    	//acctJsonUser.getJSONObject(i).put("acctPermit", "1");
    			 for(int j=0;j<loginList.size();j++) {
    				 //如果找到登录时候相匹配的 账号 并且登录的acctPermit=0 就给用户的账户赋值acctPermit=0
    				 if(loginList.getJSONObject(j).getString("acctNo").equals(acctNoUser)&&loginList.getJSONObject(j).getString("acctType").equals("PTIM")) {
    					 acctJsonUserNew.add(acctJsonUser.getJSONObject(i));
    				 }
    			 }
    		} 
    	}
		
		JSONArray SaveDArray = new JSONArray();
	  	//对新筛选出来的列表做处理
	    for(int i=0;i<acctJsonUserNew.size();i++) {
	    	switch (acctJsonUserNew.getJSONObject(i).getString("productType")) {
	        case  "0":
	        	acctJsonUserNew.getJSONObject(i).put("productTypeName", "其他");
	        	SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
	        case  "2":
	        	acctJsonUserNew.getJSONObject(i).put("productTypeName", "普通定期 ");
	        	SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;   
	        case "3":
	        	acctJsonUserNew.getJSONObject(i).put("productTypeName", "定活两便");
	        	SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "4":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "通知存款");
				SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "5":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "财运通");
				SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "6":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "小额财运通");
				SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "7":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "大额存单");
				SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "8":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "青年储蓄");
				SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "9":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "养老储蓄");
				SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
			case "10":
				acctJsonUserNew.getJSONObject(i).put("productTypeName", "成长储蓄");
				SaveDArray.add(acctJsonUserNew.getJSONObject(i));
				break;
	        } 	
	    }    	 
    	dataJson.remove("listArray");
	    dataJson.put("SaveDResultArray", SaveDArray);    
    
    	dataJson.remove("head");
	    result.remove("data");
	    result.put("data", dataJson);
	    log.info("响应"+JSONObject.fromObject(result));
		return result;
	}
    
	/**
	 * 
	 * 储蓄 -利息计算      acctNo   balance    productType  matureDate  valueDate   rate
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> getInterest(Map<String, String> reqBody) {
		log.info("利息计算请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_INTEREST.getServiceCode(), TransCodeEnum.TRANS_INTEREST.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		//这一个报文头怎么办 ================
		reqSysHead.put("userId", "99982");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		//账号做转换
		String acctSerialNo = "";
		String  acctNoUser ="";
		String acctNoStr=reqBody.get("acctNo");
		int index =acctNoStr.indexOf("-");
		if ( index> 0) {
			acctNoUser = acctNoStr.substring(0, index);
			acctSerialNo = acctNoStr.substring(index + 1, acctNoStr.length());
		} else {
			acctNoUser = acctNoStr;
			acctSerialNo = "0";
		}
		inputMap.put("acctNo", acctNoUser);        //定期账号----  6224271190391118
		inputMap.put("acctSerialNo", acctSerialNo); // 账户序号 -----106 
		inputMap.put("calculateType", "1");  //计算类型 ----  1 部提  2  销户   ---1
		inputMap.put("amt", reqBody.get("balance"));  //交易金额 ----  100
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 利息计算结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("利息计算响应"+JSONObject.fromObject(result));
			return result;
		}
		JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		String productType =reqBody.get("productType");
		Double finalIncome =0.0;//预期到期收益
		int remainDay =0;//距离到期日剩余天数
		if(productType.equals("2")) {     //只有定期存款 显示剩余天数
			//到期期限
			int term =this.daysBetween(reqBody.get("valueDate"),reqBody.get("matureDate"));
			//当前日期
			String today ="";
			Date dt =new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
			today=sdf.format(dt);
			Double  rate =Double.parseDouble( reqBody.get("rate"));
			Double  balance = Double.parseDouble(reqBody.get("balance"));
			finalIncome =AmountUtil.get2Double(term*rate*balance/36000,4);
			if(finalIncome<0.0) {
				finalIncome=0.0;
			}
			//距离到期日剩余天数
		    remainDay =this.daysBetween(today,reqBody.get("matureDate"));
			
		}else {
			remainDay =0;
			finalIncome=(Double)dataJson.get("interest");
		}
		dataJson.remove("head");
		dataJson.put("currentIncome", dataJson.get("interest"));  //当前收益
		dataJson.put("finalIncome", finalIncome);   //预期到期收益
		dataJson.put("remainDay", remainDay);   //距离到期日剩余天数
		dataJson.remove("interest");
		dataJson.remove("amtAndInt");
		result.put("data", dataJson);
		log.info("利息计算响应"+JSONObject.fromObject(result));
		return result;
	}
    
	/**
	 * 
	 * 储蓄 -查询利率  还没有esb 文档
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> getRate(Map<String, String> reqBody) {
		log.info("查询利率请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_RATE.getServiceCode(), TransCodeEnum.TRANS_RATE.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("termType", reqBody.get("termType"));        //期限类型   ----"TIM"
		inputMap.put("depTerm", reqBody.get("depTerm"));        //存期  -------"M3"
		inputMap.put("acctNo", reqBody.get("acctNo"));  //账号 
		inputMap.put("ccy", "CNY");   //币种
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 利率查询结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("查询利率响应"+JSONObject.fromObject(result));
			return result;
		}
		 JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		 dataJson.remove("head");
		 result.put("data", dataJson);
		 log.info("查询利率响应"+JSONObject.fromObject(result));
		
		return result;
	}
    
	/**
	 * 
	 * 储蓄 定转活(多加一个余额)
	 * @param reqBody
	 * @return
	 */
	@Override
	public Map<String, Object> fixToCurrent(Map<String, String> reqBody) {
		log.info("定转活请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//校验定期账户的交易密码
		reqBody.put("acctNo", reqBody.get("depositAcctNo"));
		Map<String, Object> maps = commonService.verifyTransPassword(reqBody);
		if (!CodeUtil.CODE_200.getCode().equals(maps.get("code"))) {
			maps.put("code", CodeUtil.CODE_10021.getCode());
			maps.put("msg", CodeUtil.CODE_10021.getName());
			log.info("校验交易密码 响应结果:{}", maps.get("msg"));
			return maps;
		}
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_FDTOCURRENT.getServiceCode(), TransCodeEnum.TRANS_FDTOCURRENT.getServiceSence(), 
				"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		Double  tranAmt =Double.parseDouble( reqBody.get("tranAmt"));
		Double  balance = Double.parseDouble(reqBody.get("balance"));
		if(tranAmt==balance) {
			inputMap.put("payType", "2");//支付类型  1-部分支取   2-全部支取    ------"1"
		}else {
			inputMap.put("payType", "1");//支付类型  1-部分支取   2-全部支取    ------"1"
		}
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("depositAcctNo", reqBody.get("depositAcctNo"));        //付款账号即定期账号   "6224271190391118-106"
		inputMap.put("ccy","CNY" ); //币种  送CNY
		inputMap.put("productType",reqBody.get("productType") ); //产品类型（0--其他 	1--活期 	2--普通定期 3--定活两便 4--通知存款 5--财运通 6--小额财运通 7--大额存单 8--特色储蓄(青年储蓄) 9--特色储蓄(养老储蓄) 10--特色储蓄(成长储蓄)）----"2" 
	    inputMap.put("tranAmt",reqBody.get("tranAmt") ); //交易金额（如选择全部支取必须上送定期账户的最大金额）
		inputMap.put("currentAcctNo",reqBody.get("currentAcctNo"));//收款账号即客户选择的收款账号（已加挂的）---"6224271190391118-101"
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 定转活结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("定转活响应"+JSONObject.fromObject(result));
			return result;
		}
		 JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		 dataJson.remove("head");
		 result.put("data", dataJson);
		log.info("定转活响应"+JSONObject.fromObject(result));
		return result;
	}
    
	/**
	 * 
	 * 储蓄智多薪签约
	 *
	 */
	@Override
	public Map<String, Object> signzdx(Map<String, String> reqBody) {
		log.info("智多薪签约请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		Map<String, Object> maps = commonService.verifyTransPassword(reqBody);
		if (!CodeUtil.CODE_200.getCode().equals(maps.get("code"))) {
			maps.put("code", CodeUtil.CODE_10021.getCode());
			maps.put("msg", CodeUtil.CODE_10021.getName());
			log.info("校验交易密码 响应结果:{}", maps.get("msg"));
			return maps;
		}
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_SIGNZDX.getServiceCode(), TransCodeEnum.TRANS_SIGNZDX.getServiceSence(), 
						"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		//如果查余额 根据卡号列表去核心查余额
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("operationType", "1");        //操作类型   1 签约  2解约
		inputMap.put("tranType","2" );  //交易类型      1解约      2 签约 
		inputMap.put("acctNo", reqBody.get("acctNo"));   //账号     "6224271190391122"    得到的活期账号去掉-后面的序号
		inputMap.put("tsAmt", reqBody.get("tsAmt")); // 转存金额
		inputMap.put("retainedAmt", reqBody.get("retainedAmt")); //留存金额
		//inputMap.put("agreeNo", reqBody.get("agreeNo")); // 解约用  流水号
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 智多薪签约结果
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
		    log.info("智多薪签约响应"+JSONObject.fromObject(result));
			return result;
		}
		 JSONObject dataJson = JSONObject.fromObject(result.get("data"));
		 dataJson.remove("head");
		 result.put("data", dataJson);
		 log.info("智多薪签约响应"+JSONObject.fromObject(result));
		
		return result;

	}
	
	/**
	 * 
	 * 储蓄智多薪账号查询
	 *
	 */
	@Override
	public Map<String, Object> queryZdxCard(Map<String, String> reqBody) {
		log.info("智多薪账号查询请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		Map<String, Object> saveCardList =querySaveCard(reqBody);
		if (!CodeUtil.CODE_200.getCode().equals(saveCardList.get("code"))) {
			   log.info("查询借记卡响应"+JSONObject.fromObject(saveCardList));
			   return saveCardList;
		}
	    JSONObject dataJson = JSONObject.fromObject(saveCardList.get("data"));
	    //所有账号列表
	    JSONArray acctListJson = (JSONArray) dataJson.get("listArray");
	    //非签约账号的列表
	    List<Object> noSignCardList = new ArrayList<Object>();
	    for(int i=0;i<acctListJson.size();i++) {
	    	reqBody.put("cardNo", acctListJson.getJSONObject(i).getString("cardNo"));
	    	Map<String, Object> signCardJson =queryZdxSign(reqBody);
	    	if (!CodeUtil.CODE_200.getCode().equals(signCardJson.get("code"))) {
				   log.info("智多薪账号查询响应"+JSONObject.fromObject(signCardJson));
				   return signCardJson;
			}
	    	JSONObject cardJson = JSONObject.fromObject(signCardJson.get("data"));   
	    	if (!StringUtils.isEmpty(cardJson.getString("statusType"))) {
	    		if(cardJson.getString("statusType").equals("1")) {  //筛选非签约账号  (0 签约  1  解约 )
	    			JSONObject acctOne =new JSONObject();
	    			acctOne.put("cardNo", acctListJson.getJSONObject(i).getString("cardNo"));
	    			noSignCardList.add(acctOne);
		    	}
	    	}
	    }
	    //==========写死的假数据 之后删除=====================
	    JSONObject acctOne1 =new JSONObject();
	    acctOne1.put("cardNo", "6224271190391118");
	    noSignCardList.add(acctOne1);
	    JSONObject acctOne2 =new JSONObject();
	    acctOne2.put("cardNo", "6224271190391122");
	    noSignCardList.add(acctOne2);
	    //==============写死的假数据 之后删除===============
	    dataJson.put("listArray", noSignCardList);
	    dataJson.remove("head");
	    saveCardList.put("data", dataJson);
	    log.info("智多薪账号查询响应"+JSONObject.fromObject(saveCardList));
		return saveCardList;
	}
	
	
	/**
	 * 
	 * 储蓄智多薪签约查询      CARD_NO
	 *
	 */
	@Override
	public Map<String, Object> queryZdxSign(Map<String, String> reqBody) {
		log.info("智多薪签约查询请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_QUERYSIGNZDX.getServiceCode(), TransCodeEnum.TRANS_QUERYSIGNZDX.getServiceSence(), 
						"0110", SysConstans.ABP_CODE, "");
		reqSysHead.put("userId", "99982");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("cardNo", reqBody.get("cardNo"));  //卡号 不带 -的那种
		inputMap.put("subsystem", "DHT"); //DHT-定活通   JSZ-金算子  ZNZC-智能转存  
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 储蓄智多薪签约查询结果
		log.info("智多薪签约查询响应"+JSONObject.fromObject(result));
		return result;

	}
	
	/**
	 * 
	 * 储蓄借记卡账号列表查询    
	 *
	 */
	@Override
	public Map<String, Object> querySaveCard(Map<String, String> reqBody) {
		log.info("借记卡账号列表查询请求参数【{}】", JSONObject.fromObject(reqBody).toString());
		//调用核心计价因子入参 
		Map<String, Object> reqSysHead= RequestMap.getReqSysHead(TransCodeEnum.TRANS_USERCARD.getServiceCode(), TransCodeEnum.TRANS_USERCARD.getServiceSence(), 
						"0110", SysConstans.ABP_CODE, "");
		// 获取公共参数
		Map<String, Object> inputMap = getInputMap(reqBody);
		inputMap.put("sysHead", reqSysHead);
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap); // 储蓄智多薪账号查询结果
		log.info("借记卡账号列表查询响应"+JSONObject.fromObject(result));
		return result;

	}
	
	//字符串的日期格式的计算 
    public  int daysBetween(String smdate,String bdate) {  
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
       Calendar cal = Calendar.getInstance();  
       long time1 = cal.getTimeInMillis();    
       try {
		 cal.setTime(sdf.parse(smdate));
		 cal.setTime(sdf.parse(bdate));   
	 } catch (ParseException e) {
		 log.error("距离到期日期剩余天数计算错误");
	 }    
     long time2 = cal.getTimeInMillis();         
     long between_days=(time2-time1)/(1000*3600*24);  
     return Integer.parseInt(String.valueOf(between_days));     
  }  
	
}